<?php
namespace app\admin\controller;

use app\extra\ApiToken;
use app\extra\DBCont;
use think\facade\Request;
/**
 * @title 登陆
 * @description 管理员默认账号是:admin, 密码：admin123
 * @group 报名管理后台接口
 */
class Login extends Base
{
    protected $_noAuthorization = ['create'];

    /**
     * @title 获取登陆状态
     * @description 接口说明
     * @author hardy
     * @url /admin/Login
     * @method GET
     * @header name:authorization require:1 default: desc:登陆的token
     * @return username:用户名
     * @return nickname:昵称
     * @return tel:手机号
     * @return email:邮箱
     * 
     */
    public function gets()
    {
        $user = $this->getUser();
        $this->responseSuccess($user);
    }
    /**
     * @title 用户登陆
     * @description 用户登录
     * @author hardy
     * @url /admin/Login
     * @method POST
     *
     *
     * @param name:username type:string require:1 default: other: desc:用户名初始账号为：admin
     * @param name:password type:string require:1 default: other: desc:密码,初始密码为：admin123
     *
     * @return username:用户名
     * @return nickname:昵称
     * @return tel:手机号
     * @return email:邮箱
     * 
     */
    public function create(){
        $username = input('post.username');
        if (empty($username)){
            $this->responseFail('用户名必填');
        }
        $password = input('post.password');
        if (empty($password)){
            $this->responseFail('密码必填');
        }
        $model = model('Admin');
        $data =  $model->login($username, $password);
        if ($data === false){
            $this->responseFail($model->getError());
        }
        ApiToken::$send_flg = true;
        unset($data['password']);
        $this->setUser($data);
        $this->responseSuccess($data);
    }
    /**
     * @title 修改密码
     * @description 修改密码
     * @author hardy
     * @url /admin/Login
     * @method put
     *
     *
     * @param name:password type:string require: default: other: desc:密码,参数为空则表示重置密码，重置的密码为：admin123
     */
    public function update(){
        $data['password'] =  input('put.password');
        $data['id'] = $this->getUserAttr('id');
        $model = model('Admin')->updateSave($data);
        if ($model === false){
            $this->responseFail($model->getError());
        }
        sysLogs('管理员修改密码');
        unset($model['password']);
        unset($model['id']);
        $this->responseSuccess($model);
    }
}